/****************************************************************
Program: Lawyers_Time.do
Authors: Agan, Freedman, & Owens
Paper: Is Your Lawyer a Lemon (Review of Economics and Statistics)
Updated: July 1, 2019
Description: Program to generate figures of assigned counsel 
	penalty over time for different outcomes.
Data Requirements: Requires Lawyers_CourtData.dta 
	be located in same folder as program files.
Software: Originally run in Stata-MP 14.2.
Other Notes: Calls on reghdfe.ado (version 3.2.9 21feb2016).
****************************************************************/

* Set SEs
local ses attybarcard

cap drop _I*
xi i.race
estimates drop _all
cap drop bgnum
egen bgnum=group(bg)

/* for post-fee change, move the months after July 2009 (effective date of fee change) into 2010 */
cap drop yeard
gen yeard=year
replace yeard=2010 if year==2009 & complaintmonth>=7
replace yeard=2016 if year==2015 & complaintmonth>=11

xi i.appointed*i.yeard, prefix(Y)

drop Yappointed* 
gen all_plead=plead_guilty+plead_nolo

gen xx=_n
foreach outcome in all_plead guilty dismissed defadj plead_guilty plead_nolo incarcerated reduced lnsent lnfine {
	qui reghdfe `outcome' appointed Y* male age_offense complaint_hist convict_hist hadbondsman  _I* povrate instability lndist atty_exp pct_appt racematch caseload_retained_30 if used==1 & gjuryyear>2000 & !mi(typeofoffense) & MTR==0,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old

	gen `outcome'yearapt=""
	gen `outcome'Bapt=.
	gen `outcome'SEapt=.

	 sort xx
	 cap drop i
	 gen i=2
	 replace `outcome'yearapt="05" if _n==1
	 replace `outcome'Bapt=_b[appointed] if _n==1
	 replace `outcome'SEapt=_se[appointed] if _n==1
	 
	foreach year in 06 07 08 09 10 11 12 13 14  {
		 lincom appointed+YappXyea_1_20`year'
		 replace `outcome'yearapt="`year'" if _n==i
		 replace `outcome'Bapt=r(estimate) if _n==i
		 replace `outcome'SEapt=r(se) if _n==i
		 replace i=i+1
	}

}


foreach outcome in incarcerated  lnsent lnfine {
	qui reghdfe `outcome' appointed Y* male age_offense complaint_hist convict_hist hadbondsman  _I* povrate instability lndist atty_exp pct_appt racematch caseload_retained_30 if used==1 & guilty==1 & gjuryyear>2000 & !mi(typeofoffense) & MTR==0,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old

	gen `outcome'Gyearapt=""
	gen `outcome'GBapt=.
	gen `outcome'GSEapt=.

	 sort xx
	 cap drop i
	 gen i=2
	 replace `outcome'Gyearapt="05" if _n==1
	 replace `outcome'GBapt=_b[appointed] if _n==1
	 replace `outcome'GSEapt=_se[appointed] if _n==1
	 
	 foreach year in 06 07 08 09 10 11 12 13 14 {
		 lincom appointed+YappXyea_1_20`year'
		 replace `outcome'Gyearapt="`year'" if _n==i
		 replace `outcome'GBapt=r(estimate) if _n==i
		 replace `outcome'GSEapt=r(se) if _n==i
		 replace i=i+1
	}

}

	qui reghdfe case_length appointed Y* male age_offense complaint_hist convict_hist hadbondsman  _I* povrate instability lndist atty_exp pct_appt racematch caseload_retained_30 if used==1 & MTR==0 & gjuryyear>2000 & !mi(typeofoffense) & MTR==0 ,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old

	gen case_lengthyearapt=""
	gen case_lengthBapt=.
	gen case_lengthSEapt=.

	 sort xx
	 cap drop i
	 gen i=2
	 replace case_lengthyearapt="05" if _n==1
	 replace case_lengthBapt=_b[appointed] if _n==1
	 replace case_lengthSEapt=_se[appointed] if _n==1
	 
	 foreach year in 06 07 08 09 10 11 12 13 14  {
		 lincom appointed+YappXyea_1_20`year'
		 replace case_lengthyearapt="`year'" if _n==i
		 replace case_lengthBapt=r(estimate) if _n==i
		 replace case_lengthSEapt=r(se) if _n==i
		 replace i=i+1
	}


gen post_fee=yeard>=2010 & yeard<=2015
gen post_fee2=yeard>2015

gen appt_post1=appointed*post_fee
gen appt_post2=appointed*post_fee2


foreach outcome in all_plead guilty dismissed defadj plead_guilty plead_nolo incarcerated reduced lnsent lnfine {
	qui reghdfe `outcome' appointed appt_post1 appt_post2 post_fee post_fee2 male age_offense complaint_hist convict_hist hadbondsman  _I* povrate instability lndist atty_exp pct_appt racematch caseload_retained_30 if used==1 & gjuryyear>2000 & !mi(typeofoffense) & MTR==0,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old

	gen `outcome'fee=""
	gen `outcome'Bfee=.
	gen `outcome'SEfee=.

	 sort xx
	 replace `outcome'fee="1" if _n<6
	 replace `outcome'Bfee=_b[appointed] if _n<6
	 replace `outcome'SEfee=_se[appointed] if _n<6
	 

	 lincom appointed+appt_post1
	 replace `outcome'fee="2" if _n>=6 & _n<11
	 replace `outcome'Bfee=r(estimate) if _n>=6 & _n<11
	 replace `outcome'SEfee=r(se) if _n>=6 & _n<11
	 
	  lincom appointed+appt_post2
	 replace `outcome'fee="3" if _n>=11 & _n<=13
	 replace `outcome'Bfee=r(estimate) if _n>=11 & _n<=13
	 replace `outcome'SEfee=r(se) if _n>=11 & _n<=13
}


foreach outcome in incarcerated  lnsent lnfine {
	qui reghdfe `outcome' appointed appt_post1 appt_post2 post_fee post_fee2 male age_offense complaint_hist convict_hist hadbondsman  _I* povrate instability lndist atty_exp pct_appt racematch caseload_retained_30 if used==1 & guilty==1 & gjuryyear>2000 & !mi(typeofoffense) & MTR==0,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old

	gen `outcome'Gfee=""
	gen `outcome'GBfee=.
	gen `outcome'GSEfee=.

	 sort xx
	 replace `outcome'Gfee="1" if _n<6
	 replace `outcome'GBfee=_b[appointed] if _n<6
	 replace `outcome'GSEfee=_se[appointed] if _n<6
	 

	 lincom appointed+appt_post1
	 replace `outcome'Gfee="2" if _n>=6 & _n<11
	 replace `outcome'GBfee=r(estimate) if _n>=6 & _n<11
	 replace `outcome'GSEfee=r(se) if _n>=6 & _n<11
	 
	  lincom appointed+appt_post2
	 replace `outcome'Gfee="3" if _n>=11 & _n<=13
	 replace `outcome'GBfee=r(estimate) if _n>=11 & _n<=13
	 replace `outcome'GSEfee=r(se) if _n>=11 & _n<=13
 
}


qui reghdfe case_length appointed appt_post1 appt_post2 post_fee post_fee2 male age_offense complaint_hist convict_hist hadbondsman  _I* povrate instability lndist atty_exp pct_appt racematch caseload_retained_30 if used==1 & MTR==0 &gjuryyear>2000 & !mi(typeofoffense) ,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old

	gen case_lengthfee=""
	gen case_lengthBfee=.
	gen case_lengthSEfee=.

	 sort xx
	 replace case_lengthfee="1" if _n<6
	 replace case_lengthBfee=_b[appointed] if _n<6
	 replace case_lengthSEfee=_se[appointed] if _n<6
	 

	 lincom appointed+appt_post1
	 replace case_lengthfee="2" if _n>=6 & _n<11
	 replace case_lengthBfee=r(estimate) if _n>=6 & _n<11
	 replace case_lengthSEfee=r(se) if _n>=6 & _n<11
	 
	 lincom appointed+appt_post2
	 replace case_lengthfee="3" if _n>=11 & _n<=13
	 replace case_lengthBfee=r(estimate) if _n>=11 & _n<=13
	 replace case_lengthSEfee=r(se) if _n>=11 & _n<=13

 gen effectyear=2000+real(case_lengthyearapt)
 
 foreach outcome in all_plead guilty dismissed defadj plead_guilty plead_nolo incarcerated reduced lnsent lnfine incarceratedG lnsentG lnfineG case_length {
 
	 gen `outcome'upapt=`outcome'Bapt+(1.96*`outcome'SEapt)
	 gen `outcome'downapt=`outcome'Bapt-(1.96*`outcome'SEapt)
	 
	 gen `outcome'upfee=`outcome'Bfee+(1.96*`outcome'SEfee)
	 gen `outcome'downfee=`outcome'Bfee-(1.96*`outcome'SEfee)
 
 }
 
set scheme s1mono

local reduced_title "Charge Reduced"
local dismissed_title "Case Dismissed"
local defadj_title "Def. Adjudication"
local plead_nolo_title "Plead Nolo"
local plead_guilty_title "Plead Guilty"
local guilty_title "Convicted"
local incarcerated_title "Incarceration"
local lnsent_title "Sentence Length"
local lnfine_title "Fine Amount"
local case_length_title "Case Length"
local incarceratedG_title "Incarceration | Convc."
local lnsentG_title "Sentence Length | Convc."
local lnfineG_title "Fine Amount | Convic."
local all_plead_title "Guilty & Nolo Pleas"
  
*foreach outcome in all_plead guilty dismissed defadj plead_guilty plead_nolo incarcerated reduced lnsent lnfine incarceratedG lnsentG lnfineG case_length {
foreach outcome in dismissed plead_nolo plead_guilty defadj { 
	  qui sum `outcome'downapt
	  local min=r(min)
	  local sd=r(sd)
	  local below=`min'-(`sd'/4)

   # delimit ;
	 twoway rarea `outcome'upfee `outcome'downfee effectyear if effectyear<2010 , fcolor(gs12) lcolor(gs12) ||
		 rarea `outcome'upfee `outcome'downfee effectyear if effectyear>=2010 & effectyear<2015, fcolor(gs12) lcolor(gs12) ||
		 rarea `outcome'upfee `outcome'downfee  effectyear if effectyear>=2015 , fcolor(gs12) lcolor(gs12)  ||
		 line `outcome'Bfee effectyear if effectyear<2010, lcolor(gs5) ||
		 line `outcome'Bfee effectyear if effectyear>=2010 & effectyear<2015, lcolor(gs5) ||
		 line `outcome'Bfee effectyear if effectyear>=2015 , lcolor(gs5) ||
		 rcap `outcome'upapt `outcome'downapt effectyear , lcolor(black) || 
		 scatter `outcome'Bapt effectyear , mcolor(black) msymbol(smcircle) ,
		 xline(2009.5 , lcolor(black) lpattern(dash))
		 yline(0, lcolor(black))
		 xlabel(2005(1)2014)
		 legend(off) 
		 title(``outcome'_title')
		 xtitle(" " );
	 graph save Figure3_`outcome'.gph, replace;
 # delimit cr
 
 }

